package com.boot.security.server.agency.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.boot.security.server.agency.content.AgencyConstant;
import com.boot.security.server.agency.model.param.AgencyParam;
import com.boot.security.server.agency.model.vo.AgencyRecordVo;
import com.boot.security.server.agency.model.vo.AgencyVo;
import com.boot.security.server.agency.service.AgencyRecordService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 代办
 * @author king
 */
@Slf4j
@RestController
@RequestMapping("/agency")
public class AgencyRecordController {
    @Autowired
    private AgencyRecordService agencyService;

    @RequestMapping("/findByUserNamePage")
    @ApiOperation(value = "根据用户分页查询代办信息")
    public AgencyRecordVo findByUserNamePage (HttpServletRequest request) {
        AgencyParam param = new AgencyParam();
        param.setUsername(request.getParameter("username"));
        param.setSysstate(request.getParameter("sysstate"));
        param.setPageNo(Integer.valueOf(request.getParameter("pageNo")));
        param.setPageSize(Integer.valueOf(request.getParameter("pageSize")));
        AgencyRecordVo vo = new AgencyRecordVo();
        vo.setSysstate("1");
        vo.setPageSize(param.getPageSize());
        vo.setPageNo(param.getPageNo());
        vo.setUsername(param.getUsername());
        try {
            List<AgencyVo> vos = agencyService.findByUserNamePage(param);
            vo.setResult(AgencyConstant.SUCCESS);
            vo.setMsg(AgencyConstant.QSUCCESS);
            vo.setData(JSONArray.parseArray(JSON.toJSONString(vos)));
            vo.setTotal(vos.size());
        } catch (Exception e) {
            log.info("查询代办出错", e.getMessage());
            vo.setResult(AgencyConstant.ERROR);
            vo.setMsg(String.format(AgencyConstant.QERROR, e.getMessage()));
            vo.setData(new JSONArray());
            vo.setTotal(0);
        }
        return vo;
    }
}
